<template>
  <div class="section-dialog">
    <el-dialog :title="isEdit ? '更新章节' : '添加章节'" :visible="show" @close="$emit('changeDialog', false)" width="40%">
      <el-form class="custom-form" ref="form" :model="section" label-width="80px">
        <el-form-item class="custom-form-item" label="课程名称">
          <el-input v-model="courseName" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item class="custom-form-item" label="章节名称">
          <el-input v-model="section.sectionName"></el-input>
        </el-form-item>
        <el-form-item class="custom-form-item" label="章节描述">
          <el-input v-model="section.description"></el-input>
        </el-form-item>
        <el-form-item class="custom-form-item" label="章节排序">
          <el-input v-model="section.orderNum" type="number">
              <template slot="append">数字控制排序，数字越大越靠后</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('changeDialog', false)">取消</el-button>
        <el-button type="primary" @click="changeSection"
          >确定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import Vue from 'vue'
import { createOrUpdateSection, getSectionById, CreateOrUpdateSectionType } from '@/apis/section'
export default Vue.extend({
  name: 'create-or-update-section',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    sectionId: {
      type: Number
    },
    lessonId: {
      type: Number
    },
    courseId: {
      type: [String, Number]
    },
    courseName: {
      type: String,
      default: ''
    }

  },
  data() {
    return {
      section: {} as CreateOrUpdateSectionType
    }
  },
  created() {
    this.isEdit && this.loadSection()
  },
  methods: {
    async loadSection() {
      const { data } = await getSectionById(this.sectionId)
      if (data.code === '000000') {
        const current = data.data
        this.section = {
          id: current.id,
          sectionName: current.sectionName,
          description: current.description,
          status: current.status,
          orderNum: current.orderNum
        }
      }
    },
    async changeSection() {
      const { data } = await createOrUpdateSection({ ...this.section, courseId: this.courseId })
      if (data.code === '000000') {
        this.$message.success(this.isEdit ? '更新章节成功' : '添加章节成功')
        this.$emit('changeDialog', false, true)
      }
    }
  }
})
</script>
<style lang="scss" scoped>
.section-dialog {
  .custom-form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .custom-form-item {
      width: 70%;
    }
  }
}
</style>
